package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;
import Models.Routes;

public class RoutePresenter extends AbstractPresenter<Routes> {
	final String SINGLEITEM = "routes";
	final String MULTIITEM = "routess";
	
	public ArrayList<Routes> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public Routes tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	
	public Routes createItemFromJSONObject(JSONObject jsonObject){
		Routes route = new Routes();
		try{
			route.setDayOfWeek(jsonObject.getInt("dayOfWeek"));
			route.setDescription(jsonObject.getString("routeDesc"));
			EmployeePresenter empPresenter = new EmployeePresenter();
			JSONObject jsonItem = jsonObject.getJSONObject("routeEmpID");
			route.setEmployee(empPresenter.createItemFromJSONObject(jsonItem));
			route.setEndTime(timeFormat.parse(jsonObject.getString("routeEndTime")));
			route.setTime(timeFormat.parse(jsonObject.getString("routeTime")));
			route.setId(jsonObject.getInt("routeID"));
			return route;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(Routes item){
		try{
			JSONObject json = new JSONObject();
			json.put("dayOfWeek", item.getDayOfWeek());
			json.put("routeDesc", item.getDescription());
			EmployeePresenter empPresenter = new EmployeePresenter();
			json.put("routeEmpID", empPresenter.createJSON(item.getEmployee()));
			json.put("routeEndTime", getDate(item.getEndTime()));
			json.put("routeTime", getDate(item.getTime()));
			json.put("routeID", item.getId());
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public Routes getEmptyEntity(){
		Routes entity = new Routes();
		return entity;
	}
}
